package com.example.appjava;

import com.all.qjf.changestatus.BrokerInterfaceImplServiceServiceLocator;
import com.all.qjf.changestatus.IBrokerInterfaceService;
import com.all.qjf.importbroker.ImportBrokerImplServiceServiceLocator;
import com.example.appjava.dao.SfxUser;
import com.example.appjava.dao.SfxUserDao;
import com.example.appjava.dao.User;
import com.example.appjava.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.rpc.ServiceException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;


/**
 * @author sjw
 * @className UserService
 * @date 2021/6/29 8:56
 * @description: TODO
 */
@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private SfxUserDao sfxUserDao;

    public List<User> uses(Integer age){
        return userDao.findAllByAge(age);
    }


    @Transactional
    public void test1() throws Exception {
        userDao.save(new User("a1",10));
        throw new Exception();
    }


    @Transactional
    public void test2(){
        userDao.save(new User("a2",10));
        try {
//            userDao.save(new User("a21234",10));
            throw new RuntimeException();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

    @Transactional
    public void test3(){
        userDao.save(new User("a3",10));
        userDao.save(new User("a31234",10));
    }

    @Transactional
    public void test4(){
        userDao.save(new User("a4",10));
        try {
            userDao.save(new User("a41234",10));
        }catch (Throwable e){
            System.out.println("------------------------------------------------------------");
            System.out.println(e.getMessage());
        }
    }

    void pushSfx(){
        List<SfxUser> users=  sfxUserDao.findAll();
        BrokerInterfaceImplServiceServiceLocator locator =
                new BrokerInterfaceImplServiceServiceLocator("http://60.208.116.165:6086/integrated/ws/changeStatus");
        try {
            ProvinceStatusListDTO statusListDTO = new ProvinceStatusListDTO();
            List<ProvinceStatusDTO> data=new ArrayList<>();
            ProvinceStatusDTO statusDTO=new ProvinceStatusDTO();
            data.add(statusDTO);
            statusListDTO.setData(data);
            IBrokerInterfaceService service = locator.getBrokerInterfaceImplServicePort();

            users.forEach(user->{
                String res = null;
                try {
                    Thread.sleep(1000);
                    statusDTO.setName(user.getName());
                    statusDTO.setIdnum(user.getIdcard());
                    statusDTO.setStatus(user.getStatus());
                    System.out.println(JsonHelper.objectToJson(statusListDTO));
                    res = service.changeStatus("D89F3A35931C386956C1A402A8E09941", JsonHelper.objectToJson(statusListDTO));
//                    System.out.println(JsonHelper.objectToJson(statusListDTO));
                    System.out.println(user.getName()+"    "+res);
                } catch (RemoteException | InterruptedException e) {
                    e.printStackTrace();
                }

            });


        } catch (ServiceException e) {
            e.printStackTrace();
        }
    }

    @Async
    public Future<Integer> asyncFunc() throws InterruptedException {
        System.out.println("doing");
        Thread.sleep(5000);
        System.out.println("done");
        return new AsyncResult<>(1);
    }


    int size=1;

    public String name(Integer id){
        System.out.println(id);
        User user=new User();
        user.setAge(1111);
        user.setName("111");
        userDao.save(user);
        List<Integer> a= new ArrayList<>(Collections.singletonList(id));
        a.add(2);
        for(int i=0;i<size;i++){
            a.add(i);
        }
        size++;
        userDao.findAllByIdInAndAgeIn(a,a);
        userDao.findAllByIdIn(a);
        userDao.findAllByAgeIn(a);
        String name=userDao.queryName(a);
        System.out.println(name);

        userDao.findAllByAge(10).forEach(aa->{
            System.out.println(aa.getId()+"  "+ aa.getName());
        });
        return name;
    }

}
